import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class STD {

    public static void main(String[] args) {
        Map<String, List<String>> classes = new HashMap<>();
        Map<String, Map<String, Integer>> groups = new HashMap<>(); // 小组及其评分
        Map<String, List<String>> students = new HashMap<>();

        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. 添加班级");
            System.out.println("2. 管理班级");
            System.out.println("3. 添加小组");
            System.out.println("4. 管理小组");
            System.out.println("5. 添加学生");
            System.out.println("6. 管理学生");
            System.out.println("7. 随机点名");
            System.out.println("8. 给小组评分");
            System.out.println("9. 记录学生考勤");
            System.out.println("10. 记录学生答题情况");
            System.out.println("11. 导出成绩");
            System.out.println("12. 退出");

            System.out.print("选项: ");
            int option = scanner.nextInt();

            switch (option) {
                case 1:
                    addClass(classes, scanner);
                    break;
                case 2:
                    manageClass(classes, scanner);
                    break;
                case 3:
                    addGroup(groups, scanner);
                    break;
                case 4:
                    manageGroup(groups, scanner);
                    break;
                case 5:
                    addStudent(students, scanner);
                    break;
                case 6:
                    manageStudent(students, scanner);
                    break;
                case 7:
                    randomRollCall(classes, groups, students);
                    break;
                case 8:
                    rateGroup(groups, scanner);
                    break;
                case 9:
                    recordAttendance(students, scanner);
                    break;
                case 10:
                    recordAnswering(students, scanner);
                    break;
                case 11:
                    exportScores(students, groups);
                    break;
                case 12:
                    System.out.println("退出");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效选项");
            }
        }
    }

    private static void addClass(Map<String, List<String>> classes, Scanner scanner) {
        System.out.print("添加班级名称: ");
        String className = scanner.next();
        classes.put(className, new ArrayList<>());
        System.out.println(className + " 添加成功");
    }

    private static void manageClass(Map<String, List<String>> classes, Scanner scanner) {
        System.out.println("班级表");
        for (String className : classes.keySet()) {
            System.out.println(className);
        }
    }

    private static void addGroup(Map<String, Map<String, Integer>> groups, Scanner scanner) {
        System.out.print("添加小组名称: ");
        String groupName = scanner.next();
        groups.put(groupName, new HashMap<>());
        System.out.println(groupName + " 添加成功");
    }

    private static void manageGroup(Map<String, Map<String, Integer>> groups, Scanner scanner) {
        System.out.println("小组表");
        for (String groupName : groups.keySet()) {
            System.out.println(groupName);
        }
    }

    private static void addStudent(Map<String, List<String>> students, Scanner scanner) {
        System.out.print("添加学生名字: ");
        String studentName = scanner.next();
        students.put(studentName, new ArrayList<>());
        System.out.println(studentName + " 添加成功");
    }

    private static void manageStudent(Map<String, List<String>> students, Scanner scanner) {
        System.out.println("学生表");
        for (String studentName : students.keySet()) {
            System.out.println(studentName);
        }
    }

    private static void randomRollCall(Map<String, List<String>> classes, Map<String, Map<String, Integer>> groups, Map<String, List<String>> students) {
        Random random = new Random();

        if (classes.isEmpty() || groups.isEmpty() || students.isEmpty()) {
            System.out.println("班级、小组或学生列表为空，无法进行随机点名。");
            return;
        }

        List<String> classList = new ArrayList<>(classes.keySet());
        String randomClass = classList.get(random.nextInt(classList.size()));

        List<String> groupList = new ArrayList<>(groups.keySet());
        String randomGroup = groupList.get(random.nextInt(groupList.size()));

        List<String> studentList = new ArrayList<>(students.keySet());
        String randomStudent = studentList.get(random.nextInt(studentList.size()));

        System.out.println("随机抽取");
        System.out.println("班级: " + randomClass);
        System.out.println("小组: " + randomGroup);
        System.out.println("学生: " + randomStudent);
    }

    private static void rateGroup(Map<String, Map<String, Integer>> groups, Scanner scanner) {
        System.out.print("请输入要评分的小组名称: ");
        String groupName = scanner.next();
        if (!groups.containsKey(groupName)) {
            System.out.println("该小组不存在！");
            return;
        }

        System.out.print("请输入评分: ");
        int score = scanner.nextInt();
        groups.get(groupName).put("score", score);
        System.out.println("小组 " + groupName + " 的评分已更新为: " + score);
    }

    private static void recordAttendance(Map<String, List<String>> students, Scanner scanner) {
        System.out.print("请输入学生的姓名: ");
        String studentName = scanner.next();
        if (!students.containsKey(studentName)) {
            System.out.println("该学生不存在");
            return;
        }

        System.out.print("请输入考勤状态 (出席/缺席): ");
        String attendance = scanner.next();
        students.get(studentName).add("考勤: " + attendance);
        System.out.println("学生 " + studentName + " 的考勤记录已更新: " + attendance);
    }

    private static void recordAnswering(Map<String, List<String>> students, Scanner scanner) {
        System.out.print("请输入学生的姓名: ");
        String studentName = scanner.next();
        if (!students.containsKey(studentName)) {
            System.out.println("该学生不存在");
            return;
        }

        System.out.print("请输入答题情况 (正确/错误): ");
        String answering = scanner.next();
        students.get(studentName).add("答题: " + answering);
        System.out.println("学生 " + studentName + " 的答题记录已更新: " + answering);
    }

    private static void exportScores(Map<String, List<String>> students, Map<String, Map<String, Integer>> groups) {
        System.out.println("导出成绩:");
        System.out.println("学生表:");
        for (String studentName : students.keySet()) {
            System.out.println("学生: " + studentName);
            for (String record : students.get(studentName)) {
                System.out.println(record);
            }
        }

        System.out.println("小组表:");
        for (String groupName : groups.keySet()) {
            System.out.println("小组: " + groupName);
            int score = groups.get(groupName).getOrDefault("score", 0);
            System.out.println("评分: " + score);
        }
    }
}
